<template>
  <el-breadcrumb class="!text-xs mb-4" separator="/">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    <el-breadcrumb-item>用户列表</el-breadcrumb-item>
  </el-breadcrumb>

  <el-card class="!shadow-sm">
    <el-row :gutter="10">
      <el-col :span="8">
        <el-input
          v-model="input"
          placeholder="Please input"
          class="input-with-select"
        >
          <template #append>
            <el-button :icon="Search" />
          </template>
        </el-input>
      </el-col>
      <el-col :span="16">
        <el-button type="primary">添加用户</el-button>
      </el-col>
    </el-row>

    <el-table class="mt-4" :data="userList" border stripe>
      <el-table-column label="#" type="index" align="center"></el-table-column>
      <el-table-column label="姓名" prop="username"></el-table-column>
      <el-table-column label="邮箱" prop="email"></el-table-column>
      <el-table-column label="电话" prop="mobile"></el-table-column>
      <el-table-column label="角色" prop="role_name"></el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <el-switch v-model="scope.row.mg_state"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160px">
        <template #default="scope">
          <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
            <el-button type="primary" size="small" :icon="Edit" />
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
            <el-button type="danger" size="small" :icon="Delete" />
          </el-tooltip>
          <el-tooltip effect="dark" content="分批角色" placement="top" :enterable="false">
            <el-button type="warning" size="small" :icon="Setting" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="queryInfo.pagenum"
      v-model:page-size="queryInfo.pagesize"
      :pageSizes="[2, 10, 20, 30, 40]"
      small
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>
<script lang="ts" setup>
import { ref, reactive, inject, onBeforeMount, watch } from 'vue';
import { Search, Edit, Delete, Setting } from '@element-plus/icons-vue'
import type { AxiosInstance } from 'axios'

const axios = <AxiosInstance>inject('axios')
const input = ref('')
const queryInfo = reactive({
  query: '',
  pagenum: 1,
  pagesize: 2
})
const userList = ref<user[]>([])
const total = ref<number>()

const handleSizeChange = () => {}
const handleCurrentChange = () => {}

const getUserlist = async () => {
  const { data }: apiData = await axios.get('users', { params: queryInfo })
  userList.value = data.users
  total.value = data.total
}
onBeforeMount(getUserlist)
</script>